<?php

declare (strict_types = 1);

namespace app\matter\controller;

use app\matter\BaseController;
use app\matter\model\GeneralMaterialModel;
use think\facade\Db;
use think\facade\View;

//通用材料管理
class GeneralMaterial extends BaseController
{
    //首页
    public function index()
    {

        if (request()->isAjax()) {
            $param = get_params();
            $where = array();
            if (!empty($param['keywords'])) {
                $where[] = ['title', 'like', '%' . $param['keywords'] . '%'];
            }
            //数据权限
            $rows = empty($param['limit']) ? get_config('app.page_size') : $param['limit'];
            $list = GeneralMaterialModel::where($where)->paginate($rows, false, ['query' => $param]);
            return table_assign(0, '', $list);
        } else {
            return view();
        }
    }

    // 新增/编辑
    public function add()
    {
        $param = get_params();
        if (request()->isAjax()) {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                //修改
                $Model = GeneralMaterialModel::find($id);
                $Model->save($param);
                add_log('edit', $param['id'], $param);
            } else {
                //新增
                $Model = new GeneralMaterialModel();
                $param['uid'] = $this->uid;
                $Model->save($param);
                add_log('add', $Model->id, $param);
            }
            return to_assign();
        } else {
            $id = isset($param['id']) ? $param['id'] : 0;
            if ($id > 0) {
                $detail = GeneralMaterialModel::find($id)->toArray();
                if(!empty($detail['sample_table'])){
                    $fileArray = Db::name('File')->where('id','in',$detail['sample_table'])->select();
                    $detail['sample_tableArray'] = $fileArray;
                }
                if(!empty($detail['empty_table'])){
                    $fileArray = Db::name('File')->where('id','in',$detail['empty_table'])->select();
                    $detail['empty_tableArray'] = $fileArray;
                }
                if(!empty($detail['template'])){
                    $fileArray = Db::name('File')->where('id','in',$detail['template'])->select();
                    $detail['templateArray'] = $fileArray;
                }
                View::assign('detail', $detail);
            }
            View::assign('id', $id);
            return view();
        }

    }

    // 删除
    public function delete()
    {
        $param = get_params();
        $Model = GeneralMaterialModel::find($param['id']);
        $Model->delete();
        return to_assign();

    }

    //获取通用材料信息
    public function info()
    {
        $param = get_params();
        if(!empty($param['id'])&&$param['id']>0){
            $detail = GeneralMaterialModel::find($param['id'])->toArray();
        }else{
            $detail = GeneralMaterialModel::order('sort')->find()->toArray();
        }
        if(!empty($detail['sample_table'])){
            $fileArray = Db::name('File')->where('id','in',$detail['sample_table'])->select();
            $detail['sample_tableArray'] = $fileArray;
        }
        if(!empty($detail['empty_table'])){
            $fileArray = Db::name('File')->where('id','in',$detail['empty_table'])->select();
            $detail['empty_tableArray'] = $fileArray;
        }
        if(!empty($detail['template'])){
            $fileArray = Db::name('File')->where('id','in',$detail['template'])->select();
            $detail['templateArray'] = $fileArray;
        }
        return json($detail);
    }


}
